package com.bci.pwtz.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.common.util.PwtzUtils;
import com.bci.pwtz.exception.CanNotDeleteException;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.ZydljMapper;
import com.bci.pwtz.mysql.dao.ZydljdlrefMapper;
import com.bci.pwtz.mysql.model.Zydlj;
import com.bci.pwtz.mysql.model.Zydljdlref;
import com.bci.pwtz.service.BusinessRefService;
import com.bci.pwtz.service.CompanyService;
import com.bci.pwtz.service.ZydljService;
@Service
public class ZydljServiceImpl implements ZydljService
{
    private ZydljMapper objMapper;
    private ZydljdlrefMapper refMapper;
    private BusinessRefService objService;
    private CompanyService service;
    public CompanyService getService()
    {
        return service;
    }

    public void setService(CompanyService service)
    {
        this.service = service;
    }
    
    public BusinessRefService getObjService()
    {
        return objService;
    }

    public void setObjService(BusinessRefService objService)
    {
        this.objService = objService;
    }
    private Logger logger=Logger.getLogger(ZydljServiceImpl.class);
    
    public ZydljMapper getObjMapper()
    {
        return objMapper;
    }

    public void setObjMapper(ZydljMapper objMapper)
    {
        this.objMapper = objMapper;
    }

    public ZydljdlrefMapper getRefMapper()
    {
        return refMapper;
    }

    public void setRefMapper(ZydljdlrefMapper refMapper)
    {
        this.refMapper = refMapper;
    }

    @Override
    public Zydlj add(Zydlj obj) throws PwtzDatabaseException
    {
        if(logger.isDebugEnabled()){
            logger.debug("add:obj="+obj.toString());
        }
        if(obj==null){
            if(logger.isDebugEnabled()){
                logger.debug("add,obj is null");
            }
            return null;
        }else{
            obj.setLastmodifiedtime(new Date());
            obj.setCreatedate(new Date());
            obj.setStatus(1);
        }
        try{
            objMapper.insertSelective(obj);
        }catch(DataAccessException e){
            logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return obj;
    }

    @Override
    public int update(Zydlj obj) throws PwtzDatabaseException
    {
        if(logger.isDebugEnabled()){
            logger.debug("update:obj="+obj.toString());
        }
        if(obj==null){
            if(logger.isDebugEnabled()){
                logger.debug("update,obj is null");
            }
            return 0;
        }else{
            obj.setLastmodifiedtime(new Date());
        }
        int result=0;
        try{
            result=objMapper.updateByPrimaryKeySelective(obj);
        }catch(DataAccessException e){
            logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    @Override
    public int delete(String deviceid) throws PwtzDatabaseException, CanNotDeleteException
    {
        if(logger.isDebugEnabled()){
            logger.debug("delete:deviceid="+deviceid);
        }
        int result=0;
        try{
            boolean bl=objService.canDelete("T_ZYDLJ", deviceid);
            if(!bl)
                throw new CanNotDeleteException();
            result=objMapper.deleteById(deviceid,new Date());
            if(result>0){
                List<Zydljdlref> list=refMapper.selectListByDl(deviceid,1,null,null,0,0);
                if(list!=null&&list.size()>0){
                    for(Zydljdlref obj : list){
                        refMapper.deleteByPrimaryKey(obj.getId());
                    }
                }
            }
        }catch(DataAccessException e){
            logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    @Override
    public Zydlj load(String deviceid) throws PwtzDatabaseException
    {
        if(logger.isDebugEnabled()){
            logger.debug("load:deviceid="+deviceid);
        }
        Zydlj obj=null;
        try{
            obj=objMapper.selectByPrimaryKey(deviceid);
        }catch(DataAccessException e){
            logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return obj;
    }
    public PaginationList<Zydlj> list(String devicename, Long companyid, int portalType, String[] medLineCodes, String lineCode, String beginTime, String endTime, int pageSize, int pageNo) throws PwtzDatabaseException
    {
        PaginationList<Zydlj> plist=new PaginationList<Zydlj>();
        if(logger.isInfoEnabled()){
            logger.info("list:|devicename=" + devicename+"|beginTime=" + beginTime+"|endTime=" + endTime+"|companyid="+companyid+"|portalType="+portalType+"|medLineCodes="+PwtzUtils.parseCharacterArrayToString(medLineCodes)+"|lineCode="+lineCode+"|pageSize="+pageSize+"|pageNo="+pageNo);
        }
        List<Zydlj> list=null;
        int beginIndex = pageSize * pageNo - pageSize;
        String companyidStr="";
        String [] companyids=null;
        try{
            if(portalType==1){
                companyidStr=service.findChildrensId(companyid,null);
                if(companyidStr!=null&&companyidStr.length()>0){
                    companyidStr=companyidStr.substring(0, companyidStr.length()-1);
                    companyids=companyidStr.split(",");
                }
            }
            list=objMapper.selectDeviceList(devicename,portalType,medLineCodes,lineCode,companyids,companyid,beginTime, endTime, beginIndex, pageSize);
        }catch(DataAccessException e){
            logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if(list !=null){
            logger.debug("list:result list="+list.toString());
        }
        int totalCount = 0;
        int totalPage =1;
        if(pageSize!=0){
            try
            {
                totalCount = objMapper.selectDeviceCount(devicename,portalType,medLineCodes,lineCode,companyids,companyid,beginTime,endTime);
            }
            catch (DataAccessException e)
            {
                logger.error(ExceptionUtils.getStackTrace(e));
                throw new PwtzDatabaseException();
            }
            totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        }else{
            pageNo=1;
            if(list!=null && list.size()>0){
                totalCount = list.size();
            }
        }
        plist.setTotalPage(totalPage);
        plist.setCurrPage(pageNo);
        plist.setPageSize(pageSize);
        plist.setRecords(list);
        plist.setTotalRecord(totalCount);
        return plist;
    }
}
